package byj.com.meishi;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import byj.com.meishi.bean.Person;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by Administrator on 2016/9/21 0021.
 */
public class LoginActivity extends AppCompatActivity{

    private EditText name;
    private EditText password;
    private User user;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.login_layout);
        name = (EditText) findViewById(R.id.et_name);
        password = (EditText) findViewById(R.id.et_password);
    }

    public void login(View view) {
        final String name = this.name.getText().toString().trim();
        String password = this.password.getText().toString().trim();
        BmobQuery<Person> bmobQuery = new BmobQuery<>();
        bmobQuery.addWhereLessThanOrEqualTo("name",name);
        BmobQuery<Person> bmobQuery1 = new BmobQuery<>();
        bmobQuery1.addWhereLessThanOrEqualTo("password",password);
        List<BmobQuery<Person>> queries=new ArrayList<>();
        queries.add(bmobQuery);
        queries.add(bmobQuery1);
        BmobQuery<Person> query = new BmobQuery<>();
        query.and(queries);
        query.findObjects(new FindListener<Person>() {
            @Override
            public void done(List<Person> list, BmobException e) {
                if(e==null){
                    Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                    User user = new User();
                    user.setName(name);
                    finish();
                }else {
                    Toast.makeText(LoginActivity.this,"登录失败，请检查用户名或密码",Toast.LENGTH_SHORT).show();
                }
            }
        });



    }
}
